﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using StockAccessors.Interdin.Engine;

namespace StockAccessors.Interdin.Model
{
    internal class WebItemToAccessorStockConverter
    {
        #region Attributes

        private readonly CultureInfo _interdinCultureInfo;
        private readonly NumberFormatInfo _interdinNumberInfo;
        private readonly DateTimeFormatInfo _interdinDateInfo;

        #endregion

        #region Public Methods

        public WebItemToAccessorStockConverter()
        {
            _interdinCultureInfo = System.Globalization.CultureInfo.GetCultureInfo("es");
            _interdinNumberInfo = (System.Globalization.NumberFormatInfo)_interdinCultureInfo.NumberFormat.Clone();
            _interdinDateInfo = (System.Globalization.DateTimeFormatInfo)_interdinCultureInfo.DateTimeFormat.Clone();
            _interdinNumberInfo.NumberDecimalSeparator = ".";
            _interdinNumberInfo.NumberGroupSeparator = ",";
        }

        public AccessorStockQuote ConvertItem(WebCfd item)
        {
            AccessorStockQuote result = new AccessorStockQuote();

            result.Acquisition = item.Acquisition;

            result.Symbol = item.Symbol;
            result.Name = item.Name;
			result.LastValue = item.LastValue == "-" ? 0 : decimal.Parse(item.LastValue, _interdinNumberInfo);
            result.Difference = item.Difference == "-" ? 0 : decimal.Parse(item.Difference.Replace("+",""), _interdinNumberInfo);
            result.DifferencePercentage = item.DifferencePercentage == "-" ? 0 : decimal.Parse(item.DifferencePercentage.Replace("%", "").Replace("+", ""), _interdinNumberInfo);
            result.BidVolume = item.BidVolume.TrimEnd() == "-" ? 0 : int.Parse(item.BidVolume.Replace("&nbsp;", "").Replace("[", "").Replace("]", "").Replace("&#160;", "").Trim());
			result.BidValue = item.BidValue.TrimEnd() == "-" ? 0 : decimal.Parse(item.BidValue.Replace("&nbsp;", "").Replace("&#160;", "").Trim(), _interdinNumberInfo);
            result.AskValue = item.AskValue.TrimStart() == "-" ? 0 : decimal.Parse(item.AskValue.Replace("&nbsp;", "").Replace("&#160;", "").Trim(), _interdinNumberInfo);
			result.AskVolume = item.AskVolume.TrimStart() == "-" ? 0 : int.Parse(item.AskVolume.Replace("&nbsp;", "").Replace("[", "").Replace("]", "").Replace("&#160;", "").Trim());
            result.Maximum = item.Maximum == "-" ? 0 : decimal.Parse(item.Maximum, _interdinNumberInfo);
            result.Minimum = item.Minimum == "-" ? 0 : decimal.Parse(item.Minimum, _interdinNumberInfo);
            result.Volume = item.Volume == "-" ? 0 : int.Parse(item.Volume);
			result.PreviousValue = item.PreviousValue == "-" ? 0 : decimal.Parse(item.PreviousValue, _interdinNumberInfo);
            result.Hour = DateTime.ParseExact(item.Hour, "HH:mm", _interdinDateInfo);

			// Fix wrong data coming from Interdin
			VolumeFixer.FixVolumeInQuote(result);

            return result;
        }

        public AccessorStockQuote ConvertItem(WebFuture item)
        {
            AccessorStockQuote result = new AccessorStockQuote();

            result.Acquisition = item.Acquisition;

            result.Symbol = item.Symbol;
            result.Name = item.Name;
            result.Expiry = DateTime.ParseExact(item.Expiry.TrimStart(),"MMM yyyy", _interdinDateInfo);
			result.LastValue = item.LastValue == "-" ? 0 : decimal.Parse(item.LastValue, _interdinNumberInfo);
			result.Difference = item.Difference == "-" ? 0 : decimal.Parse(item.Difference.Replace("+", ""), _interdinNumberInfo);
            result.BidVolume = item.BidVolume.TrimEnd() == "-" ? 0 : int.Parse(item.BidVolume.Replace("&nbsp;", "").Replace("[", "").Replace("]", "").Replace("&#160;", "").Trim());
			result.BidValue = item.BidValue.TrimEnd() == "-" ? 0 : decimal.Parse(item.BidValue.Replace("&nbsp;", "").Replace("&#160;", "").Trim(), _interdinNumberInfo);
            result.AskValue = item.AskValue.TrimStart() == "-" ? 0 : decimal.Parse(item.AskValue.Replace("&nbsp;", "").Replace("&#160;", "").Trim(), _interdinNumberInfo);
			result.AskVolume = item.AskVolume.TrimStart() == "-" ? 0 : int.Parse(item.AskVolume.Replace("&nbsp;", "").Replace("[", "").Replace("]", "").Replace("&#160;", "").Trim());
			result.Maximum = item.Maximum == "-" ? 0 : decimal.Parse(item.Maximum, _interdinNumberInfo); 
            result.Minimum = item.Minimum == "-" ? 0 : decimal.Parse(item.Minimum, _interdinNumberInfo);
            result.Volume = item.Volume == "-" ? 0 : int.Parse(item.Volume);
            result.PreviousValue = item.PreviousValue == "-" ? 0 : decimal.Parse(item.PreviousValue, _interdinNumberInfo);
            result.OpenInterest = item.OpenInterest == "-" ? 0 : int.Parse(item.OpenInterest);
            result.Hour = DateTime.ParseExact(item.Hour,"HH:mm", _interdinDateInfo);

			// Fix wrong data coming from Interdin
			VolumeFixer.FixVolumeInQuote(result);

            return result;
        }

        public List<AccessorStockQuote> ConvertList(List<WebCfd> items)
        {
        	return items.Select(this.ConvertItem).ToList();
        }

    	public List<AccessorStockQuote> ConvertList(List<WebFuture> items)
    	{
    		return items.Select(this.ConvertItem).ToList();
    	}

    	#endregion
    }
}
